package com.hqyj.springBoot.modules.account.controller;


import com.github.pagehelper.PageInfo;
import com.hqyj.springBoot.modules.common.vo.Result;
import com.hqyj.springBoot.modules.common.vo.Search;
import com.hqyj.springBoot.modules.account.entity.User;
import com.hqyj.springBoot.modules.account.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 127.0.0.1/api/login --- post
     */
    @PostMapping(value = "/login",consumes = MediaType.APPLICATION_JSON_VALUE)
    Result<User> login(@RequestBody User user){
        return userService.login(user);
    }

    /**
     * 127.0.0.1/api/user ----post
     * {"createDate":"2022-09-11 10:21:23","email":"1963733106@qq.com","userName":"zjz","password":"123456","userImage":"bb.jpg"}
     */
    @PostMapping(value = "/user",consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<User> insertUser(@RequestBody User user) {
        return userService.insertUser(user);
    }

    /**
     * 127.0.0.1/api/user ----put
     * {"createDate":"2022-09-11 12:00:00","email":"123@qq.com","userName":"zhang","password":"123","userImage":"kobe.jpg","userId":1}
     */
    @PutMapping(value = "/user" , consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<User> updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }

    /**
     * 127.0.0.1/api/user/userId ----delete
     */
    @DeleteMapping(value = "/user/{userId}")
    public Result<Object> deleteUserById(@PathVariable int userId){
        return userService.deleteUserById(userId);
    }

    /**
     * 127.0.0.1/api/user/1 ----get
     */
    @GetMapping(value = "/user/{userId}")
    public User selectUserById(@PathVariable int userId){
        return userService.selectUserById(userId);
    }


    /**
     * 127.0.0.1/api/users ----post
     * {"currentPage":1,"pageSize":3,"sort":"email","direction":"desc","keyword":""}
     */
    @PostMapping(value = "/users",consumes = MediaType.APPLICATION_JSON_VALUE)
    PageInfo<User> getUserBySearch(@RequestBody Search search){
        return userService.getUserBySearch(search);
    }
}
